package com.asl.website.jobPosition.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.asl.website.jobPosition.domain.JobPositionDTO;
import com.asl.website.jobPosition.domain.JobPositionDetailsDTO;
import com.asl.website.legalDocuments.domain.LegalDocumentDTO;
import com.asl.website.legalDocuments.domain.LegalDocuments;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.asl.common.annotation.Log;
import com.asl.common.core.controller.BaseController;
import com.asl.common.core.domain.AjaxResult;
import com.asl.common.enums.BusinessType;
import com.asl.website.jobPosition.domain.AslJobPosition;
import com.asl.website.jobPosition.service.IAslJobPositionService;
import com.asl.common.utils.poi.ExcelUtil;
import com.asl.common.core.page.TableDataInfo;

/**
 * 职位发布Controller
 * 
 * @author ruoyi
 * @date 2025-05-22
 */
@RestController
@RequestMapping("/jobPosition/jobPosition")
public class AslJobPositionController extends BaseController
{
    @Autowired
    private IAslJobPositionService aslJobPositionService;
    /**
     * 获取所有职位列表
     */
    @GetMapping("/content")
    public AjaxResult getAllPositions(
            @RequestHeader(value = "Accept-Language", defaultValue = "zh-CN") String acceptLanguage) {

        List<AslJobPosition> positions = aslJobPositionService.selectAslJobPositionList(new AslJobPosition());
        if (positions.isEmpty()) {
            return AjaxResult.error("暂无职位发布");
        }

        List<JobPositionDTO> result = positions.stream()
                .filter(position -> position.getStatus() == 1) // 过滤掉状态为0的职位
                .map(position -> new JobPositionDTO(position, acceptLanguage))
                .sorted(Comparator.comparingInt(JobPositionDTO::getSortOrder))
                .collect(Collectors.toList());

        if (result.isEmpty()) {
            return AjaxResult.error("暂无符合条件的职位");
        }

        return AjaxResult.success(result);
    }
    /**
     * 根据ID获取职位详情
     */
    @GetMapping("/content/{id}")
    public AjaxResult getLegalDocumentById(
            @PathVariable("id") Long id,
            @RequestHeader(value = "Accept-Language", defaultValue = "zh-CN") String acceptLanguage) {

        AslJobPosition aslJobPosition = aslJobPositionService.selectAslJobPositionById(id);
        if (aslJobPosition == null) {
            return AjaxResult.error("暂无符合条件的职位");
        }

        JobPositionDetailsDTO detailsDTO = new JobPositionDetailsDTO(aslJobPosition,acceptLanguage);
        return AjaxResult.success(detailsDTO);
    }
    /**
     * 查询职位发布列表
     */
    @PreAuthorize("@ss.hasPermi('jobPosition:jobPosition:list')")
    @GetMapping("/list")
    public TableDataInfo list(AslJobPosition aslJobPosition)
    {
        startPage();
        List<AslJobPosition> list = aslJobPositionService.selectAslJobPositionList(aslJobPosition);
        return getDataTable(list);
    }

    /**
     * 导出职位发布列表
     */
    @PreAuthorize("@ss.hasPermi('jobPosition:jobPosition:export')")
    @Log(title = "职位发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AslJobPosition aslJobPosition)
    {
        List<AslJobPosition> list = aslJobPositionService.selectAslJobPositionList(aslJobPosition);
        ExcelUtil<AslJobPosition> util = new ExcelUtil<AslJobPosition>(AslJobPosition.class);
        util.exportExcel(response, list, "职位发布数据");
    }

    /**
     * 获取职位发布详细信息
     */
    @PreAuthorize("@ss.hasPermi('jobPosition:jobPosition:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(aslJobPositionService.selectAslJobPositionById(id));
    }

    /**
     * 新增职位发布
     */
    @PreAuthorize("@ss.hasPermi('jobPosition:jobPosition:add')")
    @Log(title = "职位发布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AslJobPosition aslJobPosition)
    {
        return toAjax(aslJobPositionService.insertAslJobPosition(aslJobPosition));
    }

    /**
     * 修改职位发布
     */
    @PreAuthorize("@ss.hasPermi('jobPosition:jobPosition:edit')")
    @Log(title = "职位发布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AslJobPosition aslJobPosition)
    {
        return toAjax(aslJobPositionService.updateAslJobPosition(aslJobPosition));
    }

    /**
     * 删除职位发布
     */
    @PreAuthorize("@ss.hasPermi('jobPosition:jobPosition:remove')")
    @Log(title = "职位发布", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aslJobPositionService.deleteAslJobPositionByIds(ids));
    }
}
